package cn.net.msg.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import cn.net.msg.dao.TbExamFilesDao;
import cn.net.msg.model.ReturnNewExamFilesBean;
import cn.net.msg.model.TbExamFiles;
import cn.net.msg.model.TbExamType;
import cn.net.msg.model.TbUser;
import cn.net.msg.service.base.DaoSupport;
import cn.net.msg.service.base.Order;


@Component("tbExamFilesDao")
public class TbExamFilesDaoImpl extends DaoSupport implements TbExamFilesDao{

    private static final Log log = LogFactory.getLog(TbExamFilesDaoImpl.class);

    @Override
    public int getUserGradeId(long userid) {
        int gradeId = 0;
        if (userid == -1) {
            gradeId = 1;
        } else {
            TbUser user = super.query(TbUser.class, userid);            
            gradeId = user.getGradeId();
        }
        return gradeId;
    }

    @Override
    public List<TbExamFiles> getExamsPapersForList(int firstIndex,
            int maxResult,List<HashMap<String,Object>> queryParams, Order order, String propertyName) {
        // TODO Auto-generated method stub
        return super.paging(TbExamFiles.class, firstIndex, maxResult, queryParams, order, propertyName);
    }

    @Override
    public List<ReturnNewExamFilesBean> getNewExamPapersForList(long userid) {
        // TODO Auto-generated method stub
        List<ReturnNewExamFilesBean> newExamPapersForList = new ArrayList<ReturnNewExamFilesBean>();
        List<TbExamType> examTypeList = super.queryAll(TbExamType.class);
        if (examTypeList.size() > 0) {
            for (TbExamType tbExamType : examTypeList) {
                ReturnNewExamFilesBean returnNewExamFilesBean = new ReturnNewExamFilesBean();
                returnNewExamFilesBean.setExamTypeId(tbExamType.getExamTypeId());
                returnNewExamFilesBean.setExamTypeName(tbExamType.getExamTypeName());

                List<TbExamFiles> examFilesList = getNewExamFilesByTypeId(tbExamType.getExamTypeId(), userid);
                returnNewExamFilesBean.setExamFilesList(examFilesList);
                //如果返回考卷>0才添加到返回结果集
                if (examFilesList.size() > 0) {
                    newExamPapersForList.add(returnNewExamFilesBean);
                }
            }
        }
        return newExamPapersForList;
    }

    /**
     * 根据typeid获取最新5条记录
     * @param typeid
     * @return
     */
    private List<TbExamFiles> getNewExamFilesByTypeId(int typeid, long userid){
        List<TbExamFiles> examFilesList = new ArrayList<TbExamFiles>();

        String hql = "select t from TbExamFiles t,TbUser u where t.examTypeid = ? and t.gradeId <= u.gradeId and u.userId = ? order by t.examCreattime desc";
        List<Object> params = new ArrayList<Object>();
        params.add(typeid);
        params.add(userid);
        List<TbExamFiles> list = (List<TbExamFiles>)super.executeQuery(hql,5, params,0,0);

        if (list.size() > 0) {
            for (TbExamFiles tbExamFiles : list) {
                examFilesList.add(tbExamFiles);
            }
        }

        return examFilesList;
    }
}
